package 第五轮工作.天津理工;

import com.alibaba.excel.EasyExcel;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class TJLU {
    public static void main(String[] args) throws IOException {
        TJLUBean tjlu;
        ArrayList<TJLUBean> tjluBeans = new ArrayList<>();
        Connection connect = Jsoup.connect("https://ydq.tjut.edu.cn/xkjs/dsjs.htm");
        Document post = connect.post();
        for (Element firstRow : post.getElementsByClass("firstRow")) {
            for (Element element : firstRow.getElementsByTag("td")) {
                tjlu = new TJLUBean();
                try {
                    Element element1 = element.getElementsByTag("a").get(1);
                    if (!element1.text().contains("*")) {
                        Connection connect1 = Jsoup.connect("https://ydq.tjut.edu.cn/" + element1.attr("href").substring(3));
                        Document post1 = connect1.post();
                        for (Element vNewsContent : post1.getElementsByClass("v_news_content")) {
                            Elements td = vNewsContent.getElementsByTag("td");
                            if (td.get(9).text().equals("讲师")) {
                                continue;
                            }
                            if ((td.get(1)).text().equals("")) {
                                continue;
                            }
                            //zhicheng
//                            System.out.println(td.get(9).text());
                            tjlu.setZc(td.get(9).text());
                            //name
//                            System.out.println(td.get(1).text());
                            tjlu.setName(td.get(1).text());
                            //suoshuxueyuan
//                            System.out.println(td.get(11).text());
                            tjlu.setXi(td.get(11).text());
                            //Email
//                            System.out.println(td.get(13).text());
                            //yanjiufangxiang
//                            System.out.println(td.get(20).text());
                            tjlu.setQy("邮箱：" + td.get(13).text() + "研究方向" + td.get(20).text());
//                            System.out.println();
                        }

                    }
                } catch (Exception e) {
                    Elements element1 = element.getElementsByTag("a");
                    if (!element1.text().contains("*")) {
                        Connection connect1 = Jsoup.connect("https://ydq.tjut.edu.cn/" + element1.attr("href").substring(3));
                        Document post1 = connect1.post();
                        for (Element vNewsContent : post1.getElementsByClass("v_news_content")) {
                            Elements td = vNewsContent.getElementsByTag("td");
                            if (td.get(9).text().equals("讲师")) {
                                continue;
                            }
                            if ((td.get(1)).text().equals("")) {
                                continue;
                            }
                            //zhicheng
//                            System.out.println(td.get(9).text());
                            tjlu.setZc(td.get(9).text());
                            //name
//                            System.out.println(td.get(1).text());
                            tjlu.setName(td.get(1).text());
                            //suoshuxueyuan
//                            System.out.println(td.get(11).text());
                            tjlu.setXi(td.get(11).text());
                            //Email
//                            System.out.println(td.get(13).text());
                            //yanjiufangxiang
//                            System.out.println(td.get(20).text());
                            tjlu.setQy("邮箱：" + td.get(13).text() + "研究方向" + td.get(20).text());
//                            System.out.println();
                        }
                    }
                }
                tjluBeans.add(tjlu);
            }
        }
//        System.out.println(tjluBeans);
        EasyExcel.write("src/main/java/第五轮工作/天津理工/TJLU.xlsx", TJLUBean.class).sheet("user").doWrite(tjluBeans);
    }
}